package com.myapp.ruyixing.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.myapp.ruyixing.R;
import com.myapp.ruyixing.http.RYHttpClient;
import com.myapp.ruyixing.http.RYHttpResponseHandler;
import com.myapp.ruyixing.http.ScoreOrderParser.ScoreOrder;
import com.myapp.ruyixing.model.Message;
import com.myapp.ruyixing.ui.widget.TitleBar;
import com.myapp.ruyixing.utils.BitmapLruCache;
import com.myapp.ruyixing.utils.SafeUtils;

public class ScoreOrderFormActivity extends BaseActivity {

	private String goodsid;

	private View mLoadingView;

	private RYHttpResponseHandler mHttpResponseHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			ScoreOrder scd = (ScoreOrder) o;

			if (scd != null) {
				showView(scd);
			}

			// hideLoading();
		}

		@Override
		public void onFailure(Throwable throwable) {
			Toast.makeText(getApplicationContext(), "加载失败", Toast.LENGTH_SHORT)
					.show();

			// hideLoading();
		}
	};

	private RYHttpResponseHandler mApplyHandler = new RYHttpResponseHandler() {

		@Override
		public void onSuccess(Object o) {
			hideLoading();
			Message message = (Message) o;
			if (message != null) {
				if (message.errno == 0) {
					startActivity(new Intent(ScoreOrderFormActivity.this,
							ScoreExchangeSuccessActivity.class));
					finish();
				} else {
					Toast.makeText(ScoreOrderFormActivity.this, message.msg,
							Toast.LENGTH_SHORT).show();
				}
			}
		}

		@Override
		public void onFailure(Throwable throwable) {
			if (!isFinishing()) {
				Toast.makeText(getApplicationContext(), "加载失败",
						Toast.LENGTH_SHORT).show();
			}
			hideLoading();
		}
	};

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.activity_score_order_form);

		goodsid = getIntent().getStringExtra("goodsid");

		mLoadingView = findViewById(R.id.my_loading);
		// showLoading();

		((TitleBar) findViewById(R.id.title_bar))
				.setOnIconClickedListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						onBackPressed();
					}
				});

		RYHttpClient.getScoreOrder(this, mHttpResponseHandler, goodsid);
	}

	private void showLoading() {
		mLoadingView.setVisibility(View.VISIBLE);
	}

	private void hideLoading() {
		mLoadingView.setVisibility(View.GONE);
	}

	private void showView(final ScoreOrder data) {
		RequestQueue mQueue = Volley.newRequestQueue(this);
		ImageLoader mImageLoader = new ImageLoader(mQueue, new BitmapLruCache());

		((TextView) findViewById(R.id.title)).setText("商品名称：" + data.shopname);
		((TextView) findViewById(R.id.memberscore)).setText("您目前有积分 "
				+ data.mempoints + " 分");
		((NetworkImageView) findViewById(R.id.image)).setImageUrl(data.thumb,
				mImageLoader);
		((TextView) findViewById(R.id.change_point)).setText(data.goodspinots);
		((EditText) findViewById(R.id.contact)).setText(data.username);
		((EditText) findViewById(R.id.phone)).setText(data.phone);
		((TextView) findViewById(R.id.sex)).setText(data.sex);
		((TextView) findViewById(R.id.sex))
				.setOnClickListener(new OnClickListener() {

					@Override
					public void onClick(View v) {
						TextView sex = ((TextView) findViewById(R.id.sex));
						if (sex.getText().equals("男士")) {
							sex.setText("女士");
						} else {
							sex.setText("男士");
						}

					}
				});
		((EditText) findViewById(R.id.changecount))
				.addTextChangedListener(new TextWatcher() {

					@Override
					public void onTextChanged(CharSequence s, int start,
							int before, int count) {
						int c = SafeUtils.safeString2int(s.toString());
						((TextView) findViewById(R.id.change_point)).setText(c
								* SafeUtils.safeString2int(data.goodspinots)
								+ "");
					}

					@Override
					public void beforeTextChanged(CharSequence s, int start,
							int count, int after) {
						// TODO Auto-generated method stub

					}

					@Override
					public void afterTextChanged(Editable s) {
						// TODO Auto-generated method stub

					}
				});

		findViewById(R.id.exchange).setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showLoading();

				RYHttpClient.tranferScoreItem(ScoreOrderFormActivity.this,
						mApplyHandler, goodsid,
						((EditText) findViewById(R.id.changecount)).getText()
								.toString(),
						((EditText) findViewById(R.id.contact)).getText()
								.toString(),
						((EditText) findViewById(R.id.phone)).getText()
								.toString(),
						((EditText) findViewById(R.id.message)).getText()
								.toString());
			}
		});
	}
}
